package com.hdc.hdc.warehouseManage.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.persistence.criteria.Path;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.hdc.hdc.warehouseManage.entity.House;
import com.hdc.hdc.warehouseManage.entity.Position;
import com.hdc.hdc.warehouseManage.repository.GoodsLocationRepository;
import com.hdc.hdc.warehouseManage.repository.HouseRepository;
import com.hdc.hdc.warehouseManage.repository.PositionRepository;
import com.hdc.hdc.warehouseManage.service.WareHouseAndPositionServiceI;

/**
 * 仓库与货位服务
 * @author gxy
 *	2018年4月22日
 *	hdc.warehouseManage
 *
 */
@Service
public class WareHouseAndPositionServiceImpl implements WareHouseAndPositionServiceI {
	
	@Autowired
	private HouseRepository houseRepository;
	
	@Autowired
	private PositionRepository positionRepository;
	
	@Autowired
	private GoodsLocationRepository goodsLocationRepository;

	@Override
	public Page<House> findHousePage(int pageNum, Integer pageSize) {
		
		Pageable pg = PageRequest.of(pageNum, pageSize);
		
		return this.houseRepository.findAll(pg);
		
	}

	@Override
	public Page<Position> findPositionPage(int pageNum, Integer pageSize) {

		Pageable pg = PageRequest.of(pageNum, pageSize);
		
		return this.positionRepository.findAll(pg);
	}
	
	@Override
	public Map<String, Object> findFirstAll(int i, Integer pageSize) {
		
		Map<String, Object> result = new ConcurrentHashMap<String, Object>(30);
		
		Page<House> hp = this.findHousePage(i, pageSize);
		
//		Page<Position> pp = this.findPositionPage(i, pageSize);
		
		result.put("house", hp.getContent());
		result.put("houseSize", hp.getTotalElements());
//		result.put("position", pp.getContent()); 初始查询不用查询库存
//		result.put("positionSize", pp.getTotalElements());
		
		result.put("allHouse",this.houseRepository.findAll());
		
		return result;
	}

	@Override
	public void saveHouse(House house) {

		house.setCreateDate(new Date());
		
		this.houseRepository.save(house);
	}
	
	@Override
	public Page<Position> findPositionPage(int i, Integer pageSize, String houseId) {

		Pageable pg = PageRequest.of(i, pageSize);
		Specification<Position> spec = (root,query,cb) -> {
			
			Path<String> hp = root.get("houseId");
			
			return cb.equal(hp, houseId);
		};
		
		Specification<Position> s = Specification.where(spec);
		
		return this.positionRepository.findAll(s, pg);
	}
	
	@Override
	public void savePosition(Position position) {
		this.positionRepository.save(position);
	}
	
	@Override
	public List<House> findAllHouses() {
		return this.houseRepository.findAll();
	}
	
	@Override
	public List<Position> findAllPosition(String houseId) {
		return this.positionRepository.findByHouseId(houseId);
	}

	@Override
	public Map<String, Object> getHouseAndLocations() {

		Map<String, Object> map = new ConcurrentHashMap<>(2);
		
		map.put("house", this.findAllHouses());
		map.put("location", this.goodsLocationRepository.findAll());
		
		return map;
	}
	

}
